package cn.woblog.recyclerviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import cn.woblog.recyclerviewsample.animator.SlideInLeftAnimator;
import java.util.ArrayList;
import java.util.Random;

public class ItemAnimationActivity extends AppCompatActivity {

  private Random random;
  private UseCustomGridAdapter useCustomAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_animation);

    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
//    rv.setItemAnimator(new DefaultItemAnimator());
    SlideInLeftAnimator slideInLeftAnimator = new SlideInLeftAnimator();
    slideInLeftAnimator.setAddDuration(700);
    slideInLeftAnimator.setRemoveDuration(700);
    rv.setItemAnimator(slideInLeftAnimator);

    useCustomAdapter = new UseCustomGridAdapter(this);
    rv.setAdapter(useCustomAdapter);

    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
      data.add("Item " + i);
    }

    useCustomAdapter.setData(data);

    random = new Random();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_animation_activity, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
//    int i = random.nextInt(useCustomAdapter.getItemCount() - 1);
    int i = 1;
    switch (item.getItemId()) {
      case R.id.menu_add:

        useCustomAdapter.addData(i, "Add " + i);
        break;
      case R.id.menu_delete:
        useCustomAdapter.removeData(i);
        break;

      case R.id.menu_refresh:
        useCustomAdapter.notifyItemChanged(i);
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
